package com.ncst.backend.service.impl;

import com.ncst.backend.domain.MedicalRecord;
import com.ncst.backend.domain.vo.MedicalRecordVO;
import com.ncst.backend.mapper.MedicalRecordMapper;
import com.ncst.backend.service.MedicalRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 病历服务实现类
 */
@Slf4j
@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {

    @Autowired
    private MedicalRecordMapper medicalRecordMapper;

    @Override
    public MedicalRecord getById(Integer id) {
        return medicalRecordMapper.selectById(id);
    }

    @Override
    public List<MedicalRecord> getAll() {
        return medicalRecordMapper.selectAll();
    }

    @Override
    public List<MedicalRecord> getByCondition(MedicalRecord medicalRecord) {
        return medicalRecordMapper.selectByCondition(medicalRecord);
    }

    @Override
    public List<MedicalRecord> getByPage(Integer pageNum, Integer pageSize) {
        Integer offset = (pageNum - 1) * pageSize;
        return medicalRecordMapper.selectByPage(offset, pageSize);
    }

    @Override
    public Long countAll() {
        return medicalRecordMapper.countAll();
    }

    @Override
    public Long countByCondition(MedicalRecord medicalRecord) {
        return medicalRecordMapper.countByCondition(medicalRecord);
    }

    @Override
    public boolean save(MedicalRecordVO medicalRecordVo) {
        MedicalRecord medicalRecord = new MedicalRecord();
        BeanUtils.copyProperties(medicalRecordVo, medicalRecord);
        List<Integer> diagnosisList = medicalRecordVo.getDiagnosisList();
        int medicalRecordId = medicalRecordVo.getRegisterId();
        medicalRecordMapper.insertMedicalRecordDiseases(medicalRecordId, diagnosisList);
        try {
            return medicalRecordMapper.insert(medicalRecord) > 0;
        } catch (Exception e) {
            log.error("保存病案首页失败: {}", e.getMessage());
        }
        return false;
    }


    @Override
    public boolean saveBatch(List<MedicalRecord> list) {
        try {
            return medicalRecordMapper.insertBatch(list) > 0;
        } catch (Exception e) {
            log.error("批量保存病历失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean updateById(MedicalRecord medicalRecord) {
        try {
            return medicalRecordMapper.updateById(medicalRecord) > 0;
        } catch (Exception e) {
            log.error("更新病历失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean removeById(Integer id) {
        try {
            return medicalRecordMapper.deleteById(id) > 0;
        } catch (Exception e) {
            log.error("删除病历失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public boolean removeByIds(List<Integer> ids) {
        try {
            return medicalRecordMapper.deleteByIds(ids) > 0;
        } catch (Exception e) {
            log.error("批量删除病历失败: {}", e.getMessage());
            return false;
        }
    }

    @Override
    public MedicalRecord getByRegisterId(Integer registerId) {
        return medicalRecordMapper.selectByRegisterId(registerId);
    }

    @Override
    public boolean menZhenQueZhen(String registerId, String diagnosis, String cure) {
        return medicalRecordMapper.menZhenQueZhen(registerId, diagnosis, cure) > 0;
    }
}
